<!DOCTYPE html>
<html lang="en">

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
    <style>
        .heading td {
            background: #1D9FDF;
            color: white;
            text-align: center;
            border: none;
        }

        #heading {
            text-align: center;
        }


        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid black;
            height: 30px;
            width: 280px;
            text-align: center;
            font-weight: bold;
        }

        .noBorder td {
            border: none;
            background: lightgray;
        }

        table thead tr td {
            background: gray;
            height: 30px;
            min-width: 100px;
        }

        .reporttable {
            border-collapse: collapse;
        }
    </style>


    <script>
        var json = "";
        var checkOuLevel = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json",
                success: function (data) {
                    json = data;

                }
            });
            return json;
        };
        var district = "", sub = "", ncbc = "";
        var getFirstThreeColumn = function (ou) {
            $.ajax({
                async: false,
                type: "GET",
                url: "../api/organisationUnits/" + ou + ".json?fields=ancestors[id,name,level],displayName",
                success: function (data) {
                    for (var i = 0; i < data.ancestors.length; i++) {
                        if (data.ancestors[i].level == 2) {
                            district = data.ancestors[i].name;
                        }
                        if (data.ancestors[i].level == 3) {
                            sub = data.ancestors[i].name;
                        }
                        ncbc = data.displayName;

                    }

                }
            });
            return district, sub, ncbc;
        };

        var getPeriodArray = function (pe, ouPe) {
            var array = "";
            pe = parseInt(pe);

            while (pe != 201700) {
                array = array + pe + ";";
                pe -= 1;
            }
            array = array + ouPe;
            return array;
        };
        var month;
        var getMonth = function (a) {
            if (a == "1" || a == "01") { month = "January" }
            if (a == "2" || a == "02") { month = "February" }
            if (a == "3" || a == "03") { month = "March" }
            if (a == "4" || a == "04") { month = "April" }
            if (a == "5" || a == "05") { month = "May" }
            if (a == "6" || a == "06") { month = "June" }
            if (a == "7" || a == "07") { month = "July" }
            if (a == "8" || a == "08") { month = "August" }
            if (a == "9" || a == "09") { month = "September" }
            if (a == "10") { month = "october" }
            if (a == "11") { month = "November" }
            if (a == "12") { month = "December" }
        };

        $(document).ready(function () {
            var url = window.location.href;
            var pe1 = url.split("&")[1];
            var pe = pe1.split("=")[1];
            var monthCode = pe.substr(4, 6);
            getMonth(monthCode);
            var year = pe.substr(0, 4);
            var ou1 = url.split("&")[2];
            var ou = ou1.split("=")[1];
            var ouLeveldata = checkOuLevel(ou);
            var ouLevel = ouLeveldata.level;
            var index = 1;
            var periodArray = getPeriodArray(pe);


            $("#export").click(function (e) {
                e.preventDefault();

                //getting data from our table
                var data_type = 'data:application/vnd.ms-excel';
                var table_div = document.getElementById('report_table');
                var table_html = table_div.outerHTML.replace(/ /g, '%20');

                var a = document.createElement('a');
                a.href = data_type + ', ' + table_html;
                a.download = 'Procurement Status.xls';
                a.click();
            });

            var printData = function (ou) {
                var json1 = ""; json2 = 0;
                var col1 = "", col2 = "", col3 = "", col4 = "", col5 = "", col6 = "", col7 = "", col8 = "", col9 = "", col10 = "", col11 = "", col12 = "", col13 = "";
                var col14 = "", col1 = "", col16 = "", col17 = "", col18 = "", col19 = "", col20 = "";
                var scol1 = "", scol2 = "", scol3 = "", scol4 = "", scol5 = "", scol6 = "", scol7 = "";
                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/26/analytics/events/query/UGjSkuT89ro.json?dimension=pe:" + pe + "&dimension=ou:" + ou + "&dimension=ATwsN6WQJQq&dimension=L92irZH9UFW&dimension=BRMBUJaKTjv&dimension=Ish7K0sB961&dimension=kFw4mWPRT6n&dimension=HXOOJx6gypM&dimension=ndEyCxufHBG&dimension=UKqPNCJCkjJ&dimension=g9LMRnFMgBj&dimension=JaRX1ZaLRyw&dimension=BnjrKaUWiVv&dimension=fmSdKtJQHw8&dimension=gBt0TWdC7EB&dimension=P0TCSYtPVVT&dimension=YPYyJcgAwo8&dimension=rCoYXYKxfsj&dimension=d5ZCUyu0okQ&dimension=XVB5Sd4dsVM&dimension=uApqnwkVWiC&dimension=yZ9PE7eph3c&stage=E0btnJahvMZ&displayProperty=SHORTNAME&outputType=EVENT&skipPaging=TRUE",
                    success: function (data) {
                        json1 = data;
                        for (var i = 0; i < data.headers.length; i++) {

                            if (data.headers[i].column == "Procurement_Contract type") {
                                col1 = i;
                            }
                            if (data.headers[i].column == "Procurement_Engineers estimate") {
                                col2 = i;
                            }
                            if (data.headers[i].column == "Procurement_Proposed surface (km)") {
                                col3 = i;
                            }
                            if (data.headers[i].column == "Procurement_Contract modality (Contracts'/CP/Shopping)") {
                                col4 = i;
                            }
                            if (data.headers[i].column == "Procurement_Bid invitation date") {
                                col5 = i;
                            }
                            if (data.headers[i].column == "Procurement_Submission and opening date") {
                                col6 = i;
                            }
                            if (data.headers[i].column == "Procurement_Bid validity & bid security") {
                                col7 = i;
                            }
                            if (data.headers[i].column == "Procurement_BER Submission date to PCU") {
                                col8 = i;
                            }
                            if (data.headers[i].column == "Procurement_BER to ADB date") {
                                col9 = i;
                            }
                            if (data.headers[i].column == "Procurement_ADB NOC date") {
                                col10 = i;
                            }
                            if (data.headers[i].column == "Procurement_LOI date") {
                                col11 = i;
                            }
                            if (data.headers[i].column == "Procurement_LOA date") {
                                col12 = i;
                            }
                            if (data.headers[i].column == "Procurement_Contractor") {
                                col13 = i;
                            }
                            if (data.headers[i].column == "Procurement_Agreement date") {
                                col14 = i;
                            }
                            if (data.headers[i].column == "Procurement_Commencement date") {
                                col15 = i;
                            }
                            if (data.headers[i].column == "Procurement_Contract mobilization date") {
                                col16 = i;
                            }
                            if (data.headers[i].column == "Procurement_Intended completion date") {
                                col17 = i;
                            }
                            if (data.headers[i].column == "Procurement_Contract duration") {
                                col18 = i;
                            }
                            if (data.headers[i].column == "Procurement_Extension of Time") {
                                col19 = i;
                            }
                            if (data.headers[i].column == "Procurement_Actual completion date") {
                                col20 = i;
                            }

                        }
                    }
                });

                $.ajax({
                    async: false,
                    type: "GET",
                    url: "../api/26/analytics/events/query/UGjSkuT89ro.json?dimension=pe:" + pe + "&dimension=ou:" + ou + "&dimension=nqA0Ha062Fa&dimension=HdHIxPcoLFP&dimension=K4m81OZdVk4&dimension=rOFfGVE2rm7&dimension=OK0ERE1CRbA&dimension=rCoYXYKxfsj&dimension=BQKVbdAmZNh&dimension=T21sqK0qMuy&stage=AhjSjzbuMR5&displayProperty=SHORTNAME&outputType=EVENT&skipPaging=true",
                    success: function (data) {
                        json2 = data;
                        for (var j = 0; j < data.headers.length; j++) {

                            if (data.headers[j].column == "Procurement_Original contract amount") {
                                scol1 = j;
                            }
                            if (data.headers[j].column == "Procurement_Variation (if any)") {
                                scol2 = j;
                            }
                            if (data.headers[j].column == "Procurement_Actual contract amount") {
                                scol3 = j;
                            }
                            if (data.headers[j].column == "Procurement_Payment amount (Reporting month)") {
                                scol4 = j;
                            }
                            if (data.headers[j].column == "Procurement_Final payment amount") {
                                scol5 = j;
                            }
                            if (data.headers[j].column == "Procurement_Financial prog (%)") {
                                scol6 = j;
                            }
                            if (data.headers[j].column == "Procurement_Remarks") {
                                scol7 = j;
                            }
                        }
                    }
                });
                if (json1.rows.length == 0) { return; }
                getFirstThreeColumn(ou);

                for (var j = 0; j < json1.rows.length; j++) {
                    var row = "<tr><td>" + district + "</td><td>" + sub + "</td><td>" + ncbc + "</td>";

                    row = row + "<td>" + json1.rows[j][col1] + "</td><td>" + parseInt(json1.rows[j][col2]) + "</td><td>" + parseInt(json1.rows[j][col3]) + "</td><td>" + json1.rows[j][col4] + "</td><td>" + json1.rows[j][col5] + "</td>";
                    row = row + "<td>" + json1.rows[j][col6] + "</td><td>" + json1.rows[j][col7] + "</td><td>" + json1.rows[j][col8] + "</td><td>" + json1.rows[j][col9] + "</td><td>" + json1.rows[j][col10] + "</td>";
                    row = row + "<td>" + json1.rows[j][col11] + "</td><td>" + json1.rows[j][col12] + "</td><td>" + json1.rows[j][col13] + "</td><td>" + json1.rows[j][col14] + "</td><td>" + json1.rows[j][col15] + "</td>";
                    row = row + "<td>" + json1.rows[j][col16] + "</td><td>" + json1.rows[j][col17] + "</td><td>" + parseInt(json1.rows[j][col18]) + "</td><td>" + json1.rows[j][col19] + "</td><td>" + json1.rows[j][col20] + "</td>";

                    row = row + "<td>" + parseInt(json2.rows[j][scol1]) + "</td><td>" + parseInt(json2.rows[j][scol2]) + "</td><td>" + parseInt(json2.rows[j][scol3]) + "</td><td>" + parseInt(json2.rows[j][scol4]) + "</td><td>" + parseInt(json2.rows[j][scol5]) + "</td><td>" + parseInt(json2.rows[j][scol6]) + "</td><td>" + json2.rows[j][scol7] + "</td></tr>";
                    $('.reporttable').append(row);
                }
            };

            if (ouLevel == 1) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            var tempOu2 = tempOuData.children[l].id;
                            var tempOuData2 = checkOuLevel(tempOu2);
                            if (tempOuData2.children.length !== 0) {
                                for (var m = 0; m < tempOuData2.children.length; m++) {
                                    ouu = tempOuData2.children[m].id;
                                    printData(ouu, index);
                                    index++;
                                }
                            }
                        }
                    }
                }
            }

            else if (ouLevel == 2) {

                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    var tempOu = ouLeveldata.children[p].id;
                    var tempOuData = checkOuLevel(tempOu);
                    if (tempOuData.children.length !== 0) {
                        for (var l = 0; l < tempOuData.children.length; l++) {
                            ouu = tempOuData.children[l].id;
                            printData(ouu, index);
                            index++;
                        }
                    }
                }
            }
            else if (ouLevel == 3) {
                for (var p = 0; p < ouLeveldata.children.length; p++) {
                    ouu = ouLeveldata.children[p].id;
                    printData(ouu, index);
                    index++;
                }
            }
            else if (ouLevel == 4) {
                printData(ou);
            }


        });
    </script>
</head>

<body>
    <div id="body">
        <center><button id="export">Export to Excel</button></center>
        <br>
        <table class="table table-bordered reporttable" width="100%" id="report_table" border='1'>
            <thead>
                <tr class="heading">
                    <td colspan="5" rowspan="3"><img src="../dhis-web-commons/security/DRILP_Logo.jpg" width="90%" height="400%"></td>
                    <td colspan="20" rowspan="1">
                        <p style="font-size:16px;">Earthquake Emergency Assistance Project (EEAP)</p>
                    </td>
                    <td colspan="5" rowspan="3"></td>
                </tr>
                <tr class="heading">
                    <td colspan="20" rowspan="1">
                        <p style="font-size:20px;">Rehabilitation and Reconstruction of District Roads</p>
                    </td>
                </tr>
                <tr class="heading">
                    <td colspan="20" rowspan="1">
                        <p style="font-size:16px;">Procurement Status</p>
                    </td>
                </tr>
                <tr>
                    <td rowspan="2" colspan="1">District</td>
                    <td rowspan="2" colspan="1">Sub-project</td>
                    <td rowspan="2" colspan="1">NCB contract</td>
                    <td rowspan="2" colspan="1">Contact description</td>
                    <td rowspan="2" colspan="1">Engineer's estimate Exc. VAT cont.</td>
                    <td rowspan="2" colspan="1">Proposed surface(kms)</td>
                    <td rowspan="2" colspan="1">Contract modality</td>
                    <td rowspan="2" colspan="1">Invitation for bid (IFB)</td>
                    <td rowspan="2" colspan="1">Submission and opening date</td>
                    <td rowspan="2" colspan="1">Bid validity and bid secuirity</td>
                    <td rowspan="2" colspan="1">BER submission to PCU</td>
                    <td rowspan="2" colspan="1">BER to ADB</td>
                    <td rowspan="2" colspan="1">ADB no objection date</td>
                    <td rowspan="2" colspan="1">LOI date</td>
                    <td rowspan="2" colspan="1">LOA date</td>
                    <td rowspan="2" colspan="1">Contractor</td>
                    <td colspan="7" rowspan="1">Contract duration</td>
                    <td rowspan="2" colspan="1">Contract amount incl VAT</td>
                    <td rowspan="2" colspan="1">Variation(if any)</td>
                    <td rowspan="2" colspan="1">Contract amount after variation</td>
                    <td rowspan="2" colspan="1">Amount paid in reporting month</td>
                    <td rowspan="2" colspan="1">Final payment amount</td>
                    <td rowspan="2" colspan="1">Financial progress(%)</td>
                    <td rowspan="2" colspan="1">Remarks</td>
                </tr>
                <tr>
                    <td rowspan="1" colspan="1">Agreement date</td>
                    <td rowspan="1" colspan="1">Contrct commencement date</td>
                    <td rowspan="1" colspan="1">Contract mobilization date</td>
                    <td rowspan="1" colspan="1">Intended completion date</td>
                    <td rowspan="1" colspan="1">Contract duration</td>
                    <td rowspan="1" colspan="1">Extension of time (EOT)</td>
                    <td rowspan="1" colspan="1">Actual Completion date</td>
                </tr>
            </thead>
        </table>

    </div>
</body>

</html>